home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 July / CD 3 / redhat-6.2.iso / RedHat / instimage / usr / lib / python1.5 / site-packages / pyglade / build.py < prev    next >
Encoding:
Python Source  |  2000-02-16  |  24.5 KB  |  727 lines

  1. """This module is responsible for creating widgets from a
  2. pyglade.xmhtml.TagTree.  As opposed to the C code generation approach,
  3. this will build the widget tree at runtime.  There is nothing wrong with
  4. the code generation approach, but this one is what I have decided to do.
  5. I may write a code generator at some later point (the tag tree and style
  6. generators are sufficiently general).
  7. """
  8.  
  9. import sys
  10. import string
  11. from gtk import *
  12. # the style sheet generator
  13. import style
  14.  
  15. error = "pyglade.build.error"
  16.  
  17. # This is a dictionary of widget creation routines.
  18. # It should have string keys representing widget classes, and a tuple as
  19. # the value.  The first function is for creation of this widget type.
  20. # It should take one argument (the TagTree node), and returns a widget
  21. # with those attributes.  Note that it doesn't have to set the widget
  22. # names or connect signal handlers (in fact it shouldn't do this).
  23. #
  24. # The second function is for adding a child to this container widget.  If
  25. # this widget class is not a container, the second item in the tuple should
  26. # be None.  If it is a container, the function should take as arguments the
  27. # container, the child, and finally the 'child' TagTree node corresponding to
  28. # the child widget (or None if there is no child TagTree node).
  29.  
  30. _widgets = {}
  31.  
  32. class WidgetTree:
  33.     def __init__(self, tree):
  34.         if tree.has_key('gtk-interface'): tree = tree['gtk-interface']
  35.         if tree.tag != 'gtk-interface':
  36.             raise error, "first argument not the base of tag tree"
  37.         self.__signals = {}  # signals
  38.         self.__widgets = {}  # widgets by name
  39.         self.__paths = {}    # widgets by path
  40.         self.tooltips = None
  41.  
  42.         # parse rc strings
  43.         if tree.has_key('style'):
  44.             rc_parse_string(style.as_string(tree))
  45.  
  46.         # create widgets
  47.         if tree.has_key('widget'):
  48.             children = tree.widget
  49.             if type(children) != type(()): children = (children,)
  50.             for child in children:
  51.                 self.__new_widget(child)
  52.         if self.tooltips:
  53.             self.tooltips.enable();
  54.  
  55.     def get_widget(self, name):
  56.         if string.find(name, '.') == -1:  # no dot
  57.             return self.__widgets[name]
  58.         else:
  59.             return self.__paths[name]
  60.  
  61.     def connect(self, signame, handler, *data):
  62.         self.__signals[signame] = (self.__signals[signame][0], data,
  63.                        handler)
  64.     def disconnect(self, signame):
  65.         self.__signals[signame] = (self.__signals[signame][0], (),None)
  66.  
  67.     def __signal_handler(self, wid, *args):
  68.         signame = args[-1]
  69.         args = args[:-1]
  70.         objname, data, handler = self.__signals[signame]
  71.         if not handler: return
  72.         obj = self.__widgets[objname]
  73.         return apply(handler, (obj,) + args + data)
  74.         
  75.     def __new_widget(self, node, parent=''):
  76.         path = parent + '.' + node.name
  77.         wclass = node['class']
  78.         if wclass == 'Placeholder':
  79.             sys.stderr.write(
  80.                 "warning: placeholders still in description\n")
  81.             return
  82.         create, add = _widgets[wclass]
  83.         widget = create(node)
  84.         if node.has_key('name'): widget.set_name(node.name)
  85.         self.__widgets[node.name] = widget
  86.         self.__paths[path[:-1]] = widget
  87.         self.__set_common_params(widget, node)
  88.  
  89.         if node.has_key('signal'):
  90.             self.__setup_sighandlers(widget, node)
  91.         if node.has_key('widget'):
  92.             if not add:
  93.                 raise error, "don't know how to add " + \
  94.                       "widgets to this container"
  95.             children = node.widget
  96.             if type(children) != type(()): children = (children,)
  97.             for child in children:
  98.                 self.__new_widget(child, parent=path)
  99.                 cwidget = self.get_widget(child.name)
  100.                 add(widget, cwidget, child)
  101.  
  102.                 # these can only be set after packing
  103.                 has_default = child.get_bool('has_default',
  104.                                  FALSE)
  105.                 if has_default: cwidget.grab_default()
  106.                 has_focus = child.get_bool('has_focus', FALSE)
  107.                 if has_focus: cwidget.grab_focus()
  108.  
  109.         if node.get_bool('visible', TRUE):
  110.             widget.show()
  111.  
  112.     def __set_common_params(self, widget, node):
  113.         width = node.get_int('width', -1)
  114.         height = node.get_int('height', -1)
  115.         if width != -1 or height != -1:
  116.             widget.set_usize(width, height)
  117.         sensitive = node.get_bool('sensitive', TRUE)
  118.         if not sensitive:
  119.             widget.set_sensitive(FALSE)
  120.         tooltip = node.get('tooltip', None)
  121.         if tooltip:
  122.             if not self.tooltips: self.tooltips = GtkTooltips()
  123.             self.tooltips.set_tip(widget, tooltip, '')
  124.         can_default = node.get_bool('can_default', FALSE)
  125.         if can_default: widget.set_flags(CAN_DEFAULT)
  126.         can_focus = node.get_bool('can_focus', FALSE)
  127.         if can_focus: widget.set_flags(CAN_FOCUS)
  128.         events = node.get_int('events', 0)
  129.         if events: widget.set_events(events)
  130.         extension = node.get('extension_events', None)
  131.         if extension: widget.set_extension_events(extension)
  132.         border_width = node.get_int('border_width', 0)
  133.         if border_width: widget.set_border_width(border_width)
  134.  
  135.     def __setup_sighandlers(self, widget, node):
  136.         signals = node.signal
  137.         if type(signals) != type(()): signals = (signals,)
  138.         for sig in signals:
  139.             signame = sig.name
  140.             self.__signals[sig.handler] = (
  141.                     sig.get('object', node.name), (), None)
  142.             if sig.get_bool('after', FALSE):
  143.                 widget.connect_after(signame,
  144.                              self.__signal_handler,
  145.                              sig.handler)
  146.             else:
  147.                 widget.connect(signame, self.__signal_handler,
  148.                            sig.handler)
  149.  
  150.  
  151. # These functions handle adding elements to the various container types
  152. def container_add(cont, child, info):
  153.     cont.add(child)
  154. def box_add(box, child, info):
  155.     info = info.child
  156.     expand = info.get_bool('expand', TRUE)
  157.     fill = info.get_bool('fill', TRUE)
  158.     padding = info.get_int('padding', 0)
  159.     if info.get('pack', 'GTK_PACK_START') == 'GTK_PACK_START':
  160.         box.pack_start(child, expand=expand, fill=fill,padding=padding)
  161.     else:
  162.         box.pack_end(child, expand=expand, fill=fill,padding=padding)
  163. def table_add(table, child, info):
  164.     info = info.child
  165.     la = info.get_int('left_attach', 0)
  166.     ra = info.get_int('right_attach', 1)
  167.     ta = info.get_int('top_attach', 0)
  168.     ba = info.get_int('bottom_attach', 1)
  169.     xpad = info.get_int('xpad', 0)
  170.     ypad = info.get_int('ypad', 0)
  171.     xoptions=0
  172.     yoptions=0
  173.     if info.get_bool('xexpand', TRUE):  xoptions = xoptions | EXPAND
  174.     if info.get_bool('xshrink', FALSE): xoptions = xoptions | SHRINK
  175.     if info.get_bool('xfill', TRUE):    xoptions = xoptions | FILL
  176.     if info.get_bool('yexpand', TRUE):  yoptions = yoptions | EXPAND
  177.     if info.get_bool('yshrink', FALSE): yoptions = yoptions | SHRINK
  178.     if info.get_bool('yfill', TRUE):    yoptions = yoptions | FILL
  179.     table.attach(child, la,ra, ta,ba, xoptions=xoptions, yoptions=yoptions,
  180.              xpadding=xpad, ypadding=ypad)
  181. def fixed_add(fix, child, info):
  182.     x = info.get_int('x', 0)
  183.     y = info.get_int('y', 0)
  184.     fix.put(child, x, y)
  185. def clist_add(clist, child, info):
  186.     col = info.parent.get('col_no', 0)
  187.     info.parent['col_no'] = col + 1
  188.     clist.set_column_widget(col, child)
  189. def paned_add(paned, child, info):
  190.     pane = info.parent.get('pane2', FALSE)
  191.     info.parent['pane2'] = TRUE
  192.     if not pane:
  193.         paned.add1(child)
  194.     else:
  195.         paned.add2(child)
  196. def notebook_add(book, child, info):
  197.     pages = info.parent.pages
  198.     if not info.has_key('child_name') or info.child_name != 'Notebook:tab':
  199.         pages.append(child)
  200.     else:
  201.         # child is a label
  202.         book.append_page(pages[0], child)
  203.         del pages[0]
  204. def dialog_add(dlg, child, info):
  205.     # the widgets are already added
  206.     pass
  207.  
  208. def menuitem_add(mi, menu, info):
  209.     mi.set_submenu(menu)
  210. def menushell_add(menu, mi, info):
  211.     menu.append(mi)
  212.  
  213. def misc_set(misc, info):
  214.     xalign = info.get_float('xalign', 0.5)
  215.     yalign = info.get_float('yalign', 0.5)
  216.     misc.set_alignment(xalign, yalign)
  217.     xpad = info.get_int('xpad', 0)
  218.     ypad = info.get_int('ypad', 0)
  219.     misc.set_padding(xpad, ypad)
  220.  
  221.  
  222. def label_new(node):
  223.     str = node.get('label', '')
  224.     label = GtkLabel(str)
  225.     misc_set(label, node)
  226.     just = node.get('justify', JUSTIFY_CENTER)
  227.     label.set_justify(just)
  228.     return label
  229. def entry_new(node):
  230.     ent = GtkEntry(maxlen=node.get_int('text_max_length', -1))
  231.     if not node.get_bool('editable', TRUE):
  232.         ent.set_editable(FALSE)
  233.     if not node.get_bool('text_visible', TRUE):
  234.         ent.set_visibility(FALSE)
  235.     text = node.get('text', '')
  236.     if text: ent.set_text(text)
  237.     return ent
  238. def text_new(node):
  239.     text = GtkText()
  240.     text.set_editable(node.get_bool('editable', FALSE))
  241.     t = node.get('text', '')
  242.     if t:
  243.         text.insert_text(t)
  244.     return text
  245. def button_new(node):
  246.     label = node.get('label', '')
  247.     return GtkButton(label)
  248. def togglebutton_new(node):
  249.     label = node.get('label', '')
  250.     tog = GtkToggleButton(label)
  251.     if node.get_bool('active', FALSE):
  252.         tog.set_state(TRUE)
  253.     return tog
  254. def checkbutton_new(node):
  255.     label = node.get('label', '')
  256.     cb = GtkCheckButton(label)
  257.     if node.get_bool('active', FALSE):
  258.         cb.set_state(TRUE)
  259.     if not node.get_bool('draw_indicator', TRUE):
  260.         cb.set_mode(FALSE)
  261.     return cb
  262. def radiobutton_new(node):
  263.     label = node.get('label', '')
  264.     # do something about radio button groups ...
  265.     rb = GtkRadioButton(label)
  266.     if node.get_bool('active', FALSE):
  267.         rb.set_state(TRUE)
  268.     if not node.get_bool('draw_indicator', TRUE):
  269.         rb.set_mode(FALSE)
  270.     return rb
  271. def optionmenu_new(node):
  272.     omenu = GtkOptionMenu()
  273.     menu = GtkMenu()
  274.     for item in string.split(node.get('items', ''), '\n'):
  275.         mi = GtkMenuItem(item)
  276.         menu.append(mi)
  277.         mi.show()
  278.     omenu.set_menu(menu)
  279.     return omenu
  280. def combo_new(node):
  281.     combo = GtkCombo()
  282.     if node.get_bool('case_sensitive', FALSE):
  283.         combo.set_case_sensitive(TRUE)
  284.     if not node.get_bool('use_arrows', TRUE):
  285.         combo.set_use_arrows(FALSE)
  286.     if node.get_bool('use_arrows_always', FALSE):
  287.         combo.set_use_arrows_always(TRUE)
  288.     return combo
  289. def list_new(node):
  290.     list = GtkList()
  291.     mode = node.get('selection_mode', SELECTION_SINGLE)
  292.     list.set_selection_mode(mode)
  293.     return list
  294. def clist_new(node):
  295.     numcols = node.get_int('columns', 1)
  296.     clist = GtkCList(numcols)
  297.     widths = node.get('column_widths', None)
  298.     if widths:
  299.         widths = map(string.atoi, string.split(widths, ','))
  300.         for i in range(numcols):
  301.             clist.set_column_width(i, widths[i])
  302.     if node.get_bool('show_titles', TRUE):
  303.         clist.column_titles_show()
  304.     mode = node.get('selection_mode', SELECTION_SINGLE)
  305.     clist.set_selection_mode(mode)
  306.     #shadow = node.get('shadow_type', SHADOW_IN)
  307.     #clist.set_border(shadow)
  308.     #hpol = node.get('hscrollbar_policy', POLICY_ALWAYS)
  309.     #vpol = node.get('vscrollbar_policy', POLICY_ALWAYS)
  310.     #clist.set_policy(hpol, vpol)
  311.     return clist
  312. def tree_new(node):
  313.     tree = GtkTree()
  314.     mode = node.get('selection_mode', SELECTION_SINGLE)
  315.     tree.set_selection_mode(mode)
  316.     mode = node.get('view_mode', TREE_VIEW_LINE)
  317.     tree.set_view_mode(mode)
  318.     if not node.get_bool('view_line', TRUE):
  319.         tree.set_view_lines(FALSE)
  320.     return tree
  321. def spinbutton_new(node):
  322.     climb_rate = node.get_int('climb_rate', 1)
  323.     digits = node.get_int('digits', 0)
  324.     hvalue = node.get_float('hvalue', 1)
  325.     hlower = node.get_float('hlower', 0)
  326.     hupper = node.get_float('hupper', 100)
  327.     hstep  = node.get_float('hstep', 1)
  328.     hpage  = node.get_float('hpage', 10)
  329.     hpage_size = node.get_float('hpage_size', 10)
  330.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  331.     spin = GtkSpinButton(adj=adj, climb_rate=climb_rate, digits = digits)
  332.     spin.set_numeric(node.get_bool('numeric', FALSE))
  333.     pol = node.get('update_policy', UPDATE_IF_VALID)
  334.     spin.set_update_policy(pol)
  335.     spin.set_snap_to_ticks(node.get_bool('snap', FALSE))
  336.     spin.set_wrap(node.get_bool('wrap', FALSE))
  337.     return spin
  338. def hscale_new(node):
  339.     hvalue = node.get_float('hvalue', 1)
  340.     hlower = node.get_float('hlower', 0)
  341.     hupper = node.get_float('hupper', 100)
  342.     hstep  = node.get_float('hstep', 1)
  343.     hpage  = node.get_float('hpage', 10)
  344.     hpage_size = node.get_float('hpage_size', 10)
  345.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  346.     scale = GtkHScale(adj)
  347.     scale.set_draw_value(node.get_bool('draw_value', TRUE))
  348.     scale.set_value_pos(node.get('value_pos', POS_TOP))
  349.     scale.set_digits(node.get_int('digits', 1))
  350.     scale.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  351.     return scale
  352. def vscale_new(node):
  353.     hvalue = node.get_float('hvalue', 1)
  354.     hlower = node.get_float('hlower', 0)
  355.     hupper = node.get_float('hupper', 100)
  356.     hstep  = node.get_float('hstep', 1)
  357.     hpage  = node.get_float('hpage', 10)
  358.     hpage_size = node.get_float('hpage_size', 10)
  359.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  360.     scale = GtkVScale(adj)
  361.     scale.set_draw_value(node.get_bool('draw_value', TRUE))
  362.     scale.set_value_pos(node.get('value_pos', POS_TOP))
  363.     scale.set_digits(node.get_int('digits', 1))
  364.     scale.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  365.     return scale
  366. def hruler_new(node):
  367.     widget = GtkHRuler()
  368.     widget.set_metric(node.get('metric', PIXELS))
  369.     lower = node.get_int('lower', 0)
  370.     upper = node.get_int('upper', 10)
  371.     pos = node.get_int('position', 0)
  372.     max = node.get_int('max_size', 10)
  373.     widget.set_range(lower, upper, pos, max)
  374.     return widget
  375. def vruler_new(node):
  376.     widget = GtkVRuler()
  377.     widget.set_metric(node.get('metric', PIXELS))
  378.     lower = node.get_int('lower', 0)
  379.     upper = node.get_int('upper', 10)
  380.     pos = node.get_int('position', 0)
  381.     max = node.get_int('max_size', 10)
  382.     widget.set_range(lower, upper, pos, max)
  383.     return widget
  384. def hscrollbar_new(node):
  385.     hvalue = node.get_float('hvalue', 1)
  386.     hlower = node.get_float('hlower', 0)
  387.     hupper = node.get_float('hupper', 100)
  388.     hstep  = node.get_float('hstep', 1)
  389.     hpage  = node.get_float('hpage', 10)
  390.     hpage_size = node.get_float('hpage_size', 10)
  391.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  392.     scroll = GtkHScrollbar(adj)
  393.     scroll.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  394.     return scroll
  395. def vscrollbar_new(node):
  396.     hvalue = node.get_float('hvalue', 1)
  397.     hlower = node.get_float('hlower', 0)
  398.     hupper = node.get_float('hupper', 100)
  399.     hstep  = node.get_float('hstep', 1)
  400.     hpage  = node.get_float('hpage', 10)
  401.     hpage_size = node.get_float('hpage_size', 10)
  402.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  403.     scroll = GtkVScrollbar(adj)
  404.     scroll.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  405.     return scroll
  406. def statusbar_new(node):
  407.     return GtkStatusbar()
  408. def toolbar_new(node):
  409.     orient = node.get('orientation', ORIENTATION_HORIZONTAL)
  410.     style = node.get('type', TOOLBAR_ICONS)
  411.     tool = GtkToolbar(orient, style)
  412.     tool.set_space_size(node.get_int('space_size', 5))
  413.     tool.set_tooltips(node.get_bool('tooltips', TRUE))
  414.     return tool
  415. def progressbar_new(node):
  416.     return GtkProgressBar()
  417. def arrow_new(node):
  418.     dir = node.get('arrow_type', ARROW_RIGHT)
  419.     shadow = node.get('shadow_type', SHADOW_OUT)
  420.     arr = GtkArrow(dir, shadow)
  421.     misc_set(arr, node)
  422.     return arr
  423. # image not supported
  424. def pixmap_new(node):
  425.     # the first parameter needs to be the toplevel widget
  426.     pix,bit = create_pixmap_from_xpm(None, None, node.get('filename', ''))
  427.     pixmap = GtkPixmap(pix, bit)
  428.     misc_set(pixmap)
  429.     return pixmap
  430. def drawingarea_new(node):
  431.     return GtkDrawingArea()
  432. def hseparator_new(node):
  433.     return GtkHSeparator()
  434. def vseparator_new(node):
  435.     return GtkVSeparator()
  436.  
  437. def menubar_new(node):
  438.     return GtkMenuBar()
  439. def menu_new(node):
  440.     return GtkMenu()
  441. def menuitem_new(node):
  442.     if node.has_key('label'):
  443.         ret = GtkMenuItem(node.label)
  444.     else:
  445.         ret = GtkMenuItem()
  446.     if node.get_bool('right_justify', FALSE):
  447.         ret.right_justify()
  448.     return ret
  449. def checkmenuitem_new(node):
  450.     ret = GtkCheckMenuItem(node.label)
  451.     if node.get_bool('right_justify', FALSE):
  452.         ret.right_justify()
  453.     if node.get_bool('active', FALSE):
  454.         ret.set_state(TRUE)
  455.     if node.get_bool('always_show_toggle', FALSE):
  456.         ret.set_show_toggle(TRUE)
  457.     return ret
  458. def radiomenuitem_new(node):
  459.     ret = GtkRadioMenuItem(node.label)
  460.     if node.get_bool('right_justify', FALSE):
  461.         ret.right_justify()
  462.     if node.get_bool('active', FALSE):
  463.         ret.set_state(TRUE)
  464.     if node.get_bool('always_show_toggle', FALSE):
  465.         ret.set_show_toggle(TRUE)
  466.     return ret
  467.  
  468. def hbox_new(node):
  469.     if node.has_key('child_name') and node.child_name[:7] == 'Dialog:':
  470.         return node.__wid
  471.     homogeneous = node.get_bool('homogeneous', FALSE)
  472.     spacing = node.get_int('spacing', 0)
  473.     return GtkHBox(homogeneous=homogeneous, spacing=spacing)
  474. def vbox_new(node):
  475.     if node.has_key('child_name') and node.child_name[:7] == 'Dialog:':
  476.         return node.__wid
  477.     homogeneous = node.get_bool('homogeneous', FALSE)
  478.     spacing = node.get_int('spacing', 0)
  479.     return GtkVBox(homogeneous=homogeneous, spacing=spacing)
  480. def table_new(node):
  481.     rows = node.get_int('rows', 1)
  482.     cols = node.get_int('columns', 1)
  483.     homog = node.get_bool('homogeneous', FALSE)
  484.     table = GtkTable(rows, cols, homog)
  485.     table.set_row_spacings(node.get_int('row_spacing', 0))
  486.     table.set_col_spacings(node.get_int('col_spacing', 0))
  487.     return table
  488. def fixed_new(node):
  489.     return GtkFixed()
  490. def hbuttonbox_new(node):
  491.     bbox = GtkHButtonBox()
  492.     layout = node.get('layout_style', BUTTONBOX_DEFAULT_STYLE)
  493.     bbox.set_layout(layout)
  494.     spacing = node.get_int('child_min_width', None)
  495.     if spacing: bbox.set_spacing(spacing)
  496.     width, height = bbox.get_child_size_default()
  497.     width = node.get_int('child_min_width', width)
  498.     height = node.get_int('child_min_height', height)
  499.     bbox.set_child_size(width, height)
  500.     ipx, ipy = bbox.get_child_ipadding_default()
  501.     ipx = node.get_int('child_ipad_x', ipx)
  502.     ipy = node.get_int('child_ipad_y', ipy)
  503.     bbox.set_child_ipadding(ipx, ipy)
  504.     return bbox
  505. def vbuttonbox_new(node):
  506.     bbox = GtkVButtonBox()
  507.     layout = node.get('layout_style', BUTTONBOX_DEFAULT_STYLE)
  508.     bbox.set_layout(layout)
  509.     spacing = node.get_int('child_min_width', None)
  510.     if spacing: bbox.set_spacing(spacing)
  511.     width, height = bbox.get_child_size_default()
  512.     width = node.get_int('child_min_width', width)
  513.     height = node.get_int('child_min_height', height)
  514.     bbox.set_child_size(width, height)
  515.     ipx, ipy = bbox.get_child_ipadding_default()
  516.     ipx = node.get_int('child_ipad_x', ipx)
  517.     ipy = node.get_int('child_ipad_y', ipy)
  518.     bbox.set_child_ipadding(ipx, ipy)
  519.     return bbox
  520. def frame_new(node):
  521.     label = node.get('label', '')
  522.     frame = GtkFrame(label)
  523.     xalign = node.get_int('label_xalign', 0)
  524.     frame.set_label_align(xalign, 0.5)
  525.     shadow = node.get('shadow_type', SHADOW_ETCHED_IN)
  526.     frame.set_shadow_type(shadow)
  527.     return frame
  528. def aspectframe_new(node):
  529.     xalign = node.get_int('xalign', 0)
  530.     yalign = node.get_int('yalign', 0)
  531.     ratio = node.get_float('ratio', 1)
  532.     obey = node.get_bool('obey_child', TRUE)
  533.     frame = GtkAspectFrame(xalign, yalign, ratio, obey_child)
  534.     label = node.get('label', '')
  535.     if label: frame.set_label(label)
  536.     xalign = node.get_int('label_xalign', 0)
  537.     frame.set_label_align(xalign, 0.5)
  538.     shadow = node.get('shadow_type', SHADOW_ETCHED_IN)
  539.     frame.set_shadow_type(shadow)
  540.     return frame
  541. def hpaned_new(node):
  542.     paned = GtkHPaned()
  543.     handle_size = node.get_int('handle_size', 0)
  544.     if handle_size: paned.set_handle_size(handle_size)
  545.     gutter_size = node.get_int('gutter_size', 0)
  546.     if gutter_size: paned.set_gutter_size(gutter_size)
  547.     return paned
  548. def vpaned_new(node):
  549.     paned = GtkVPaned()
  550.     handle_size = node.get_int('handle_size', 0)
  551.     if handle_size: paned.set_handle_size(handle_size)
  552.     gutter_size = node.get_int('gutter_size', 0)
  553.     if gutter_size: paned.set_gutter_size(gutter_size)
  554.     return paned
  555. def handlebox_new(node):
  556.     return GtkHandleBox()
  557. def notebook_new(node):
  558.     book = GtkNotebook()
  559.     book.set_show_tabs(node.get_bool('show_tabs', TRUE))
  560.     book.set_show_border(node.get_bool('show_border', TRUE))
  561.     book.set_tab_pos(node.get('tab_pos', POS_TOP))
  562.     book.set_scrollable(node.get_bool('scrollable', FALSE))
  563.     book.set_tab_border(node.get_int('tab_border', 3))
  564.     book.popup_enable(node.get_bool('popup_enable', FALSE))
  565.     node['pages'] = []
  566.     return book
  567. def alignment_new(node):
  568.     xalign = node.get_float('xalign', 0.5)
  569.     yalign = node.get_float('yalign', 0.5)
  570.     xscale = node.get_float('xscale', 0)
  571.     yscale = node.get_float('yscale', 0)
  572.     return GtkAlignment(xalign, yalign, xscale, yscale)
  573. def eventbox_new(node):
  574.     return GtkEventBox()
  575. def scrolledwindow_new(node):
  576.     win = GtkScrolledWindow()
  577.     hpol = node.get('hscrollbar_policy', POLICY_ALWAYS)
  578.     vpol = node.get('vscrollbar_policy', POLICY_ALWAYS)
  579.     win.set_policy(hpol, vpol)
  580.     # do something about update policies here ...
  581.     return win
  582. def viewport_new(node):
  583.     port = GtkViewport()
  584.     shadow = node.get('shadow_type', SHADOW_IN)
  585.     port.set_shadow_type(shadow)
  586.     return port
  587.  
  588. def curve_new(node):
  589.     curve = GtkCurve()
  590.     curve.set_curve_type(node.get('curve_type', CURVE_TYPE_SPLINE))
  591.     minx = node.get_float('min_x', 0)
  592.     maxx = node.get_float('max_x', 1)
  593.     miny = node.get_float('min_y', 0)
  594.     maxy = node.get_float('max_y', 1)
  595.     curve.set_range(minx, maxx, miny, maxy)
  596.     return curve
  597. def gammacurve_new(node):
  598.     gamma = GtkGammaCurve()
  599.     # do something about the curve parameters ...
  600.     return gamma
  601. def colorselection_new(node):
  602.     cs = GtkColorSelection()
  603.     cs.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  604.     return cs
  605. def preview_new(node):
  606.     type = node.get('type', PREVIEW_COLOR)
  607.     prev = GtkPreview(type)
  608.     prev.set_expand(node.get_bool('expand', TRUE))
  609.     return prev
  610.  
  611. def window_new(node):
  612.     wintype = node.get('type', 'toplevel')
  613.     widget = GtkWindow(wintype)
  614.     widget.set_title(node.get('title', node.name))
  615.     x = node.get_int('x', -1)
  616.     y = node.get_int('y', -1)
  617.     if x != -1 or y != -1:
  618.         widget.set_uposition(x, y)
  619.     pos = node.get('position', None)
  620.     if pos: widget.set_position(pos)
  621.     ashrink = node.get_bool('allow_shrink', TRUE)
  622.     agrow = node.get_bool('allow_grow', TRUE)
  623.     autoshrink = node.get_bool('auto_shrink', FALSE)
  624.     if not ashrink or not agrow or autoshrink:
  625.         widget.set_policy(ashrink, agrow, autoshrink)
  626.     return widget
  627. def dialog_new(node):
  628.     widget = GtkDialog()
  629.     widget.set_title(node.get('title', node.name))
  630.     x = node.get_int('x', -1)
  631.     y = node.get_int('y', -1)
  632.     if x != -1 or y != -1:
  633.         widget.set_uposition(x, y)
  634.     pos = node.get('position', None)
  635.     if pos: widget.set_position(pos)
  636.     ashrink = node.get_bool('allow_shrink', TRUE)
  637.     agrow = node.get_bool('allow_grow', TRUE)
  638.     autoshrink = node.get_bool('auto_shrink', FALSE)
  639.     if not ashrink or not agrow or autoshrink:
  640.         widget.set_policy(ashrink, agrow, autoshrink)
  641.     # do some weird stuff because the vbox/action area is already created
  642.     node.widget['__wid'] = widget.vbox
  643.     children = node.widget.widget
  644.     if type(children) != type(()): children = (children,)
  645.     for i in range(len(children)):
  646.         if children[i].has_key('child_name') and \
  647.            children[i].child_name == 'Dialog:action_area':
  648.             node['widget'] = (node.widget, children[i])
  649.             children[i].parent = node
  650.             children[i]['__wid'] = widget.action_area
  651.             node.widget[0]['widget'] = children[0:i]+children[i+1:]
  652.             break
  653.     return widget
  654. def colorselectiondialog_new(node):
  655.     return GtkColorSelectionDialog()
  656.  
  657.  
  658. _widgets = {
  659.     # widgets ...
  660.     'GtkLabel':        (label_new,        None),
  661.     'GtkEntry':        (entry_new,        None),
  662.     'GtkText':         (text_new,         None),
  663.     'GtkButton':       (button_new,       None),
  664.     'GtkToggleButton': (togglebutton_new, None),
  665.     'GtkCheckButton':  (checkbutton_new,  None),
  666.     'GtkRadioButton':  (radiobutton_new,  None),
  667.     'GtkOptionMenu':   (optionmenu_new,   None),
  668.     'GtkCombo':        (combo_new,        None),
  669.     'GtkList':         (list_new,         None),
  670.     'GtkCList':        (clist_new,        clist_add),
  671.     'GtkTree':         (tree_new,         None),
  672.     'GtkSpinButton':   (spinbutton_new,   None),
  673.     'GtkHScale':       (hscale_new,       None),
  674.     'GtkVScale':       (vscale_new,       None),
  675.     'GtkHRuler':       (hruler_new,       None),
  676.     'GtkVRuler':       (vruler_new,       None),
  677.     'GtkHScrollbar':   (hscrollbar_new,   None),
  678.     'GtkVScrollbar':   (vscrollbar_new,   None),
  679.     'GtkStatusbar':    (statusbar_new,    None),
  680.     'GtkToolbar':      (toolbar_new,      None),
  681.     'GtkProgressBar':  (progressbar_new,  None),
  682.     'GtkArrow':        (arrow_new,        None),
  683.     'GtkPixmap':       (pixmap_new,       None),
  684.     'GtkDrawingArea':  (drawingarea_new,  None),
  685.     'GtkHSeparator':   (hseparator_new,   None),
  686.     'GtkVSeparator':   (vseparator_new,   None),
  687.  
  688.     # Menu stuff ...
  689.     'GtkMenuBar':      (menubar_new,      menushell_add),
  690.     'GtkMenu':         (menu_new,         menushell_add),
  691.     'GtkMenuItem':     (menuitem_new,     menuitem_add),
  692.     'GtkCheckMenuItem':(checkmenuitem_new,menuitem_add),
  693.     'GtkRadioMenuItem':(radiomenuitem_new,menuitem_add),
  694.  
  695.     # Containers ...
  696.     'GtkHBox':         (hbox_new,         box_add),
  697.     'GtkVBox':         (vbox_new,         box_add),
  698.     'GtkTable':        (table_new,        table_add),
  699.     'GtkFixed':        (fixed_new,        fixed_add),
  700.     'GtkHButtonBox':   (hbuttonbox_new,   container_add),
  701.     'GtkVButtonBox':   (vbuttonbox_new,   container_add),
  702.     'GtkFrame':        (frame_new,        container_add),
  703.     'GtkAspectFrame':  (aspectframe_new,  container_add),
  704.     'GtkHPaned':       (hpaned_new,       paned_add),
  705.     'GtkVPaned':       (vpaned_new,       paned_add),
  706.     'GtkHandleBox':    (handlebox_new,    container_add),
  707.     'GtkNotebook':     (notebook_new,     notebook_add),
  708.     'GtkAlignment':    (alignment_new,    container_add),
  709.     'GtkEventBox':     (eventbox_new,     container_add),
  710.     'GtkScrolledWindow': (scrolledwindow_new, container_add),
  711.     'GtkViewport':     (viewport_new,     container_add),
  712.  
  713.     # Miscellaneous ...
  714.     'GtkCurve':        (curve_new,        None),
  715.     'GtkGammaCurve':   (gammacurve_new,   None),
  716.     'GtkColorSelection': (colorselection_new, None),
  717.     'GtkPreview':      (preview_new,      None),
  718.  
  719.     # Windows ...
  720.     'GtkWindow':       (window_new,       container_add),
  721.     'GtkDialog':       (dialog_new,       dialog_add),
  722.     'GtkColorSelectionDialog': (colorselectiondialog_new, None),
  723. }
  724.  
  725.  
  726.  
  727.